package com.base.library.coroutine

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlin.coroutines.CoroutineContext

private class IOScope(override val coroutineContext: CoroutineContext = Dispatchers.IO) :
    CoroutineScope

private class IOSupervisorScope(override val coroutineContext: CoroutineContext = Dispatchers.IO + SupervisorJob()) :
    CoroutineScope

private val ioSupervisorScope: IOSupervisorScope by lazy { IOSupervisorScope() }
private val ioScope: IOScope by lazy { IOScope() }

/**
 * start io coroutine
 */
fun io(block: suspend CoroutineScope.() -> Unit) =
    ioScope.launch {
        block()
    }

/**
 * start io coroutine
 */
fun ioSupervisor(block: suspend CoroutineScope.() -> Unit) =
    ioSupervisorScope.launch {
        block()
    }